<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  confirm-text="打印"
  title="标本采集"
  :on-close="updateModel"
  :on-cancel="updateModel"
  size="small"
)
  template(#footer)
      el-button(@click="updateModel") 取消
      el-button(type="primary" @click="printBarcode") 打印条码
      el-button(type="primary" :loading="loadingMap.batchGenerateBarcode" @click="completeCollection") 采集完成
  .collection
    .collection__left
      el-descriptions(:column="1")
        el-descriptions-item(label="业务编码:")
          span(style="font-weight: 600;color: black") {{viewingObj?.outpatientNumber}}
        el-descriptions-item(label="患者姓名:")
          span(style="font-weight: 600;color: black") {{viewingObj.patientName}}
        el-descriptions-item(label="检查项目:")
          span(style="font-weight: 600;color: black") {{viewingObj.applicationProject }}
        el-descriptions-item(label="标本类型:")
          span(style="font-weight: 600;color: black") {{viewingObj.specimenTypeName}}
        el-descriptions-item(label="标本容器:")
          span(style="font-weight: 600;color: black") {{viewingObj.sampleContainer}}
        el-descriptions-item(label="采集方式:")
          span(style="font-weight: 600;color: black") {{data.loadcollectionMethod?.find(i=>i.value==viewingObj.collectionMethod)?.label }}
        el-descriptions-item(label="采集规格:")
          span(style="font-weight: 600;color: black") {{viewingObj.specs }}
        el-descriptions-item(label="标本量:")
          span(style="font-weight: 600;color: black") {{ viewingObj.bloodCollectionVolume}}
    .collection__right
      el-image(style="width: 80px; height: 80px ;margin-left:50px" :src="viewingObj.imgUrl" )
      el-image(v-for="(item,index) in barcodeList" :src="item" :key="index")
  barCode(v-model="baeCodeShow" :selectionRow="selectionRow" )
</template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import { useNotify } from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
import barCode from "./barCode.vue";
import { ESpecimenStatus } from "@scmt/api-common";

//添加 Props属性
const props = defineProps({
  selectionRow: {
    // 这里需要传入标本号一致的数组
    type: Array as PropType<SpecimenEntity[]>,
    default: () => [],
  },
});
const $api = useApi();
const $notify = useNotify();

const appmodel = defineModel({
  default: false,
});
const baeCodeShow = ref(false);
const barcodeList = ref<string[]>([]);
const viewingObj = ref<SpecimenEntity>($api.lis.specimen.$$createEntity());
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const { selectionRow } = toRefs(props);

const printBarcode = () => {
  if (selectionRow.value.some((i) => i.paymentStatus === 0)) {
    $notify.warning("存在未缴费项目，请先核对缴费状态后在进行检查!!");
    return;
  }
  baeCodeShow.value = true;
};

const completeCollection = () => {
  request("batchGenerateBarcode", {
    specimenList: selectionRow.value.map((i) => i.id),
    status: ESpecimenStatus.Collection,
  });
};

const { data, request, loadingMap } = useRequest({
  listAsOptions: () => {
    return $api.lis.specimen.category.listAsOptions();
  },
  loadcollectionMethod: () => $api.dict.item.listAsOptions("collection_method"),
  batchGenerateBarcode: (q: any) => {
    return $api.lis.specimen.batchGenerateBarcode(q).then(() => {
      $notify.success("采集成功");
      appmodel.value = false;
      emit("message-to-parent");
    });
  },
  printBarcode: (val: SpecimenEntity[]) => {
    return $api.lis.specimen.printBarcode(val).then((res) => {
      if (res) {
        barcodeList.value = res.datalist;
      }
    });
  },
});

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      const firstSpecimen = selectionRow.value[0];
      if (
        selectionRow.value.length > 1 &&
        selectionRow.value.some((i) => i.barcode !== firstSpecimen.barcode)
      ) {
        $notify.warning("请选择同一业务编码的标本进行采集");
      }
      viewingObj.value = Object.assign(
        {},
        $api.lis.specimen.$$createEntity(firstSpecimen),
        {
          applicationProject: selectionRow.value
            .map((i) => i.applicationProject)
            .join(";"),
        }
      );
      request("listAsOptions");
      request("loadcollectionMethod");
      request("printBarcode", selectionRow.value);
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped>
.collection {
  height: 100%;
  display: flex;

  &__left {
    flex: 5;
  }

  &__right {
    margin-left: var(--mt-margin);
    margin-left: 100px;
    flex: 5;
  }
}
</style>
